/**
 * 将javascript数据类型转换为json字符串的方法。
 *
 * @public
 * @param {object} 需转换为json字符串的对象, 一般为Json 【支持object,array,string,function,number,boolean,regexp *】
 * @return 返回json字符串
 **/
jQuery.extend({
    toJSONString: function (object) {
        var type = typeof object;
        if ('object' == type) {
            if (object != null && Array == object.constructor) type = 'array';
            else if (object != null && RegExp == object.constructor) type = 'regexp';
            else type = 'object';
        }
        switch (type) {
            case'undefined':
            case'unknown':
                return;
                break;
            case'function':
            case'boolean':
            case'regexp':
                return object.toString();
                break;
            case'number':
                return isFinite(object) ? object.toString() : 'null';
                break;
            case'string':
                return'"' + object.replace(/(\\|\")/g, "\\$1").replace(/\n|\r|\t/g, function () {
                    var a = arguments[0];
                    return (a == '\n') ? '\\n' : (a == '\r') ? '\\r' : (a == '\t') ? '\\t' : ""
                }) + '"';
                break;
            case'object':
                if (object === null) return'null';
                var results = [];
                for (var property in object) {
                    var value = jQuery.toJSONString(object[property]);
                    if (value !== undefined) results.push(jQuery.toJSONString(property) + ':' + value);
                }
                return'{' + results.join(',') + '}';
                break;
            case'array':
                var results = [];
                for (var i = 0; i < object.length; i++) {
                    var value = jQuery.toJSONString(object[i]);
                    if (value !== undefined) results.push(value);
                }
                return'[' + results.join(',') + ']';
                break;
        }
    },
    toJSONStringPrefix: function (object, prefix) {
        var type = typeof object;
        if ('object' == type) {
            if (object != null && Array == object.constructor) type = 'array';
            else if (object != null && RegExp == object.constructor) type = 'regexp';
            else type = 'object';
        }
        switch (type) {
            case'undefined':
            case'unknown':
                return;
                break;
            case'function':
            case'boolean':
            case'regexp':
                return object.toString();
                break;
            case'number':
                return isFinite(object) ? object.toString() : 'null';
                break;
            case'string':
                return'"' + object.replace(/(\\|\")/g, "\\$1").replace(/\n|\r|\t/g, function () {
                    var a = arguments[0];
                    return (a == '\n') ? '\\n' : (a == '\r') ? '\\r' : (a == '\t') ? '\\t' : ""
                }) + '"';
                break;
            case'object':
                if (object === null) return'null';
                var results = [];
                for (var property in object) {
                    var value = jQuery.toJSONString(object[property]);
                    if (value !== undefined) results.push(jQuery.toJSONString(prefix + property) + ':' + value);
                }
                return'{' + results.join(',') + '}';
                break;
            case'array':
                var results = [];
                for (var i = 0; i < object.length; i++) {
                    var value = jQuery.toJSONString(object[i]);
                    if (value !== undefined) results.push(value);
                }
                return'[' + results.join(',') + ']';
                break;
        }
    },
    toJSONStringSubPrefix: function (object, prefix, prefixPorperty) {
        var type = typeof object;
        if ('object' == type) {
            if (object != null && Array == object.constructor) type = 'array';
            else if (object != null && RegExp == object.constructor) type = 'regexp';
            else type = 'object';
        }
        switch (type) {
            case'undefined':
            case'unknown':
                return;
                break;
            case'function':
            case'boolean':
            case'regexp':
                return object.toString();
                break;
            case'number':
                return isFinite(object) ? object.toString() : 'null';
                break;
            case'string':
                return'"' + object.replace(/(\\|\")/g, "\\$1").replace(/\n|\r|\t/g, function () {
                    var a = arguments[0];
                    return (a == '\n') ? '\\n' : (a == '\r') ? '\\r' : (a == '\t') ? '\\t' : ""
                }) + '"';
                break;
            case'object':
                if (object === null) return'null';
                var results = [];
                for (var property in object) {
                    var flag = false;
                    var value = jQuery.toJSONString(object[property]);
                    for (var i = 0; i < prefixPorperty.length; i++) {

                        //朱强添加转变让取值名和实际值名
                        var porpertyOldName = prefixPorperty[i];
                        var porpertyNewName = prefixPorperty[i];
                        if (typeof(prefixPorperty[i]) == "object") {
                            var temp = prefixPorperty[i];
                            porpertyOldName = temp[0];
                            porpertyNewName = temp[1];
                        }

                        if (porpertyOldName == property) {
                            flag = true;
                            break;
                        } else
                            flag = false;
                    }
                    if (flag) {
                        if (value !== undefined) results.push(jQuery.toJSONString(prefix + porpertyNewName) + ':' + value);
                    }
                }
                return'{' + results.join(',') + '}';
                break;
            case'array':
                var results = [];
                for (var i = 0; i < object.length; i++) {
                    var value = jQuery.toJSONString(object[i]);
                    if (value !== undefined) results.push(value);
                }
                return'[' + results.join(',') + ']';
                break;
        }
    }
});

